<?php
/**
 * @annotate 职位管理类
 * @group   Admin/Company
 * @author   yachtxiao <fuck@fackyou.org>
 */
class HiringAction extends BaseAction {
	protected $aVerify = array(
   	'list_hiring',
   	'add_hiring',
   	'edit_hiring',
   	'doadd_hiring',
   	'refresh_hiring',
   	'del_hiring',
   	'del_template',
   	'getposition',
   	'ontop',
   	'list_temp',
   	'edit_template',
   	'save_template',
  	);//需要验证的方法
	public function list_hiring(){
		$this->assign("Title","职位管理");
		$hirings = D('Hiring')->get_hiring_list();
		$menu = D('Company_menu')->select_menu();
		$this->assign('refresh_num',$menu['refresh']);
		$this->assign('stick_num',$menu['stick']);
		$this->assign('hirings',$hirings);
		$this->display();
	}

	public function add_hiring() {
		$this->assign("Title","添加职位");
		$company_info = D('Company')->getinfo();
		$stores = D('Company')->list_store();
		if(empty($company_info['brand'])){
			$this->Yes = 1;
	        $this->Suce = "请先绑定品牌";
	        $this->Jurl = U('Setting/brand');
			//$this->error('请先绑定品牌',U('Setting/brand'));
			//exit();
		}
		if(empty($stores)){
			$this->Yes = 1;
	        $this->Suce = "请先添加门店";
	        $this->Jurl = U('Setting/store');
			//$this->error('请先添加门店',U('Setting/store'));
			//exit();
		}
		$manage = D('Company')->get_manage_list();
		if(empty($manage)){
			$this->Yes = 1;
	        $this->Suce = "请先绑定微信管理员";
	        $this->Jurl = U('Setting/wxbind');
			//$this->error('请先绑定微信管理员',U('Setting/wxbind'));
			//exit();
		}
		$menu = D('Company_menu')->select_menu();
		$this->assign('send_job_num',$menu['job_num']);
		if($menu['job_num'] == 0){
			$this->Yes = "-1";
	        $this->Err = "您发布职位的次数已经使用完，请及时充值，或联系我们！";
			//exit;
		}
		$this->assign('manage',$manage);
		$this->assign('hangye',gethangye());
		$this->assign('edu',getedu());
		$this->assign('height',C('HEIGHT_CONFIG'));
		$this->assign('exper',C('EXPER_CONFIG'));
		$this->assign('worktime',getworktime());
		$this->assign('workplace',format_workplace($stores));
		$templates = D('Hiring')->get_hiring_template();
		$this->assign('templates',$templates);
		$this->assign('brand',format_brand($company_info['brand']));
		$this->display();
	}
	
	public function edit_hiring() {
		$this->assign("Title","修改职位");
		$company_info = D('Company')->getinfo();
		$stores = D('Company')->list_store();
		$hid = $_GET['hiring_id'];
		$list = D('Hiring')->get_hiring_list($hid);
		$manage = D('Company')->get_manage_list();
		$this->assign('manage',$manage);
		$this->assign('list',$list);
		$this->assign('hangye',gethangye());
		$this->assign('edu',getedu());
		$this->assign('height',C('HEIGHT_CONFIG'));
		$this->assign('exper',C('EXPER_CONFIG'));
		$this->assign('worktime',getworktime());
		$this->assign('workplace',format_workplace($stores));
		$templates = D('Hiring')->get_hiring_template();
		$this->assign('templates',$templates);
		$position = D('position')->where('is_del = 0 AND hid = '.$list['hid'])->select();
		$this->assign('posi',$position);
		$this->assign('brand',format_brand($company_info['brand']));
		$this->display();
	}

	public function doadd_hiring(){
		$menu = D('Company_menu')->select_menu();
		if($menu['job_num'] == 0){
			$this->Yes = "-1";
	        $this->Err = "您发布职位的次数已经使用完，请及时充值，或联系我们！";
			exit;
		}
		$data = $_POST;
		$data['template'][$data['template_id']]['notice'] = implode(',', $data['template'][$data['template_id']]['notice']);
		$map = $data;
		if($data['template_id']==0){
			$map['template_id'] = D('Hiring')->add_hiring_template($data['template'][$data['template_id']]);
		}
		$data['template'][$data['template_id']]['hiring_id'] = D('Hiring')->add_hiring($map);
		$result = D('Hiring')->add_hiring_info($data['template'][$data['template_id']]);
		if($result){

			if($data['hiring_id']==""){
				$result1 = D('Company_menu')->send_job();
				if($result1){
					$this->success("添加成功！",U('Hiring/list_hiring'));	
				}
				
			}else{
				$this->success("修改成功！");	
			}
			
		}else{
			if($data['hiring_id']==""){
				$this->success("添加失败！");	
			}else{
				$this->success("修改失败！");	
			}
		}
	}
	
	public function save_hiring(){
		$hiring_id = $_GET['hiring_id'];

		$data = $_POST;
		$data['info']['notice'] = implode(',', $data['info']['notice']);
		$result_hiring = D('Hiring')->save_hiring($data,$hiring_id);
		$result_hiring_info = D('Hiring')->save_hiring_info($data['info'],$hiring_id);
		if($result_hiring&&$result_hiring_info){
			$this->success("修改成功！",U('Hiring/list_hiring'));	
		}else{
			$this->error("修改失败！");	
		}
	}
	
	public function refresh_hiring(){
		$menu = D('Company_menu')->select_menu();
		if($menu['refresh'] == 0){
			$this->ajaxReturn('','您刷新职位的次数已经使用完，请及时充值，或联系我们！',0);
			
		}else{
			$hid = $_POST['hid'];
			$data['updateline'] = time();
			$result_hiring = D('hiring')->where('hiring_id = '.$hid)->save($data);
			$result_hiring_info = D('hiring_info')->where('hiring_id = '.$hid)->save($data);
			if($result_hiring&&$result_hiring_info){
				$result = D('Company_menu')->refresh_job();
				if($result){
					$this->ajaxReturn('','刷新成功',1);
				}
			}else{
				$this->ajaxReturn('','刷新失败',0);
			}
		}
		
	}
	public function del_hiring(){
		$hid = $_POST['hid'];
		$data['is_del'] = 1;
		$result = D('hiring')->where('hiring_id = '.$hid)->save($data);
		if($result){
			$this->ajaxReturn('','删除成功',1);
		}else{
			$this->ajaxReturn('','删除失败',0);
		}
	}

	public function del_template(){
		$tid = $_POST['tid'];
		$data['is_del'] = 1;
		$result = D('hiring_template')->where('template_id = '.$tid)->save($data);
		if($result){
			$this->ajaxReturn('','删除成功',1);
		}else{
			$this->ajaxReturn('','删除失败',0);
		}
	}
	
	public function getposition(){
		$hid = $_POST['hid'];
		$position = D('position')->where('is_del = 0 AND hid = '.$hid)->select();
		exit(json_encode($position));
	}
	
	public function ontop(){
		$menu = D('Company_menu')->select_menu();
		if($menu['stick'] == 0){
			$this->ajaxReturn('','您刷新职位的次数已经使用完，请及时充值，或联系我们！',0);
			
		}else{
			$data['ontop'] = $_POST['ontop'];
			$data['ontop_time'] = time();
			$result = D('hiring')->where('hiring_id = '.$_POST['hid'])->save($data);
			if($result){
				$result1 = D('Company_menu')->stick_job();
				if($result1){
					$this->ajaxReturn('','操作成功',1);
				}
			}else{
				$this->ajaxReturn('','操作失败',0);
			}
		}
	}
	public function list_temp(){
		$this->assign("Title","模板列表");
		$templates = D('Hiring')->get_hiring_template();
		$this->assign('templates',$templates);
		$this->display();
	}
	public function edit_template(){
		$this->assign("Title","修改模板");
		$tid = $_GET['template_id'];
		$templates = D('Hiring')->get_hiring_template($tid);
		$this->assign('tem',$templates);
		$this->assign('edu',getedu());
		$manage = D('Company')->get_manage_list();
		$this->assign('manage',$manage);
		$this->assign('height',C('HEIGHT_CONFIG'));
		$this->assign('exper',C('EXPER_CONFIG'));
		$this->assign('worktime',getworktime());
		$this->display();
	}
	public function save_template(){
		$data = array(
			'template_name'=>$_POST['template_name'],
			'salary'=>$_POST['salary'],
			'edu'=>$_POST['edu'],
			'age'=>$_POST['age_start'].','.$_POST['age_start'],
			'sex'=>$_POST['sex'],
			'height'=>$_POST['height'],
			'exper'=>$_POST['exper'],
			'worktime'=>$_POST['worktime'],
			'welfare'=>$_POST['welfare'],
			'intro'=>$_POST['intro'],
			'notice'=>implode(',', $_POST['notice']),
			'updateline'=>time()
			);
		$list = D('hiring_template')->where('template_id ='.$_POST['tid'])->save($data);
		if($list){
			$this->success('修改成功！','__URL__/list_temp');
		}else{
			$this->error("修改失败！");
		}
	}
	
}
?>